package com.lp.compt.services;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import com.lp.compt.repositories.SsNoticeEntityRepository;
import com.lp.compt.utility.SFWhere;
import com.lp.compt.pojo.SsNoticeEntity;

import java.util.Calendar;

/**
 * 服务类
 *
 * @author
 * @since 1.0.0
 */
@Service
public class SsNoticeEntityService {

    @Autowired
    SsNoticeEntityRepository ssNoticeEntityRepository;

    /**
     * 根据SsNoticeEntity的字段自动生成条件,字段值为null不生成条件
     * 如果是数值型的字段,前端不传入值,默认是0,例如ID的类型是Long,如果不传值,默认是0
     * 可以自己设置下SFWhere.and(ssNoticeEntity).equal(实体.getId()>0,"id",实体.getId()).build()
     *
     * @param ssNoticeEntity 实体对象
     * @param pageable       分页对象
     * @return 返回分页\状态码
     */
    public ResponseEntity<Object> search(SsNoticeEntity ssNoticeEntity, Pageable pageable) {
        Page<SsNoticeEntity> all = ssNoticeEntityRepository.findAll(SFWhere.and(ssNoticeEntity)
                        .build(),
                PageRequest.of(
                        pageable.getPageNumber(),
                        pageable.getPageSize(),
                        Sort.by(Sort.Direction.DESC,
                                "nTime")
                )
        );
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    public void save(SsNoticeEntity ssNoticeEntity) {
        ssNoticeEntity.setnUser("管理员");
        ssNoticeEntity.setnTime(Calendar.getInstance().getTime());
        ssNoticeEntityRepository.save(ssNoticeEntity);
    }

    public void del(SsNoticeEntity ssNoticeEntity) {
//        SsNoticeEntity entity = ssNoticeEntityRepository.getOne(ssNoticeEntity.getnId());
        ssNoticeEntityRepository.delete(ssNoticeEntity);
    }
}
